package com.pjchat.pjchatclient.frame;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.ita.im.model.Unit;
import com.ita.im.model.UnitType;
import com.ita.im.model.User;
import com.pjchat.pjchatclient.handler.Handler;
import com.pjchat.pjchatclient.handlerfactory.HandlerFactory;
import com.pjchat.pjchatclient.receiver.LoginReceiverRunnable;
import com.pjchat.pjchatclient.validator.PasswordValidator;

public class MyLoginFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	private BufferedReader br;
	private BufferedWriter bw;

	private JLabel myTitle;
	private JLabel myNameLabel;
	private JTextField myName;
	private JLabel myPasswordLabel;
	private JTextField myPassword;
	private JButton myBtnLogin;
	private JButton myBtnRegister;
	private JLabel myResult;
	private JLabel myBackground;

	public MyLoginFrame(BufferedReader br, BufferedWriter bw)
			throws IOException {
		this.br = br;
		this.bw = bw;
		init();
		loadListener();

		new Thread(new LoginReceiverRunnable(this, br, bw)).start();
		setVisible(true);
	}

	private void init() throws IOException {

		setSize(300, 400);
		setResizable(false);
		setTitle("PJChat");
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		setLocationRelativeTo(null);
		setLayout(null);
		setIconImage(new ImageIcon("kaixi.png").getImage());

		myTitle = new JLabel();
		myNameLabel = new JLabel();
		myName = new JTextField();
		myPasswordLabel = new JLabel();
		myPassword = new JTextField();
		myBtnLogin = new JButton();
		myBtnRegister = new JButton();
		myResult = new JLabel();
		myBackground = new JLabel(new ImageIcon("bg.jpg"));

		add(myTitle);
		add(myNameLabel);
		add(myName);
		add(myPasswordLabel);
		add(myPassword);
		add(myBtnLogin);
		add(myBtnRegister);
		add(myResult);
		add(myBackground);

		myTitle.setBounds(100, 10, 200, 80);
		myTitle.setFont(new Font("font1", 6, 30));
		myTitle.setText("LOGIN");
		myTitle.setForeground(Color.WHITE);

		myNameLabel.setBounds(30, 100, 230, 30);
		myNameLabel.setFocusable(false);
		myNameLabel.setFont(new Font("font1", 6, 15));
		myNameLabel.setText("Name:");
		myNameLabel.setForeground(Color.WHITE);

		myName.setBounds(30, 125, 230, 30);
		myName.setEditable(true);
		myName.setFocusable(true);
		myName.setFont(new Font("font1", 6, 15));

		myPasswordLabel.setBounds(30, 160, 230, 30);
		myPasswordLabel.setFocusable(false);
		myPasswordLabel.setFont(new Font("font1", 6, 15));
		myPasswordLabel.setText("Password:");
		myPasswordLabel.setForeground(Color.WHITE);

		myPassword.setBounds(30, 185, 230, 30);
		myPassword.setEditable(true);
		myPassword.setFocusable(true);
		myPassword.setFont(new Font("font1", 6, 15));

		myBtnLogin.setBounds(40, 250, 100, 30);
		myBtnLogin.setText("Login");

		myBtnRegister.setBounds(150, 250, 100, 30);
		myBtnRegister.setText("Register");

		myResult.setBounds(40, 280, 250, 40);
		myResult.setFont(new Font("font1", 6, 20));
		myResult.setForeground(Color.WHITE);

		myBackground.setBounds(0, 0, 300, 400);
	}

	public static void main(String[] args) throws IOException {
		MyLoginFrame mlf = new MyLoginFrame(null, null);
	}

	private void loadListener() {
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		myBtnLogin.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {

			}

			@Override
			public void mousePressed(MouseEvent e) {

			}

			@Override
			public void mouseExited(MouseEvent e) {

			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseClicked(MouseEvent e) {
				Handler handler = HandlerFactory.produce(UnitType.USER_LOGIN);
				Unit result = new Unit();
				Map<String, Object> map = new HashMap<String, Object>();
				User me = new User();
				me.setName(myName.getText());
				me.setPassword(myPassword.getText());
				map.put("user", me);
				result.setType(UnitType.USER_LOGIN);
				result.setValue(map);
				result.setSuccess(true);
				handler.doRequest(MyLoginFrame.this, result, br, bw);
			}
		});

		myBtnRegister.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				Handler handler = HandlerFactory
						.produce(UnitType.USER_REGISTER);
				Unit result = new Unit();
				Map<String, Object> map = new HashMap<String, Object>();
				User me = new User();
				me.setName(myName.getText());
				if (PasswordValidator.checkPassword(myPassword.getText())) {
					me.setPassword(myPassword.getText());
				} else {
					myResult.setText("password:0-9,a-z,A-Z,@,_");
					return;
				}
				map.put("user", me);
				result.setType(UnitType.USER_REGISTER);
				result.setValue(map);
				result.setSuccess(true);
				handler.doRequest(MyLoginFrame.this, result, br, bw);
			}
		});
	}

	public void switchFrame() {
		this.setVisible(false);
	}

	public JLabel getMyTitle() {
		return myTitle;
	}

	public void setMyTitle(JLabel myTitle) {
		this.myTitle = myTitle;
	}

	public JLabel getMyNameLabel() {
		return myNameLabel;
	}

	public void setMyNameLabel(JLabel myNameLabel) {
		this.myNameLabel = myNameLabel;
	}

	public JTextField getMyName() {
		return myName;
	}

	public void setMyName(JTextField myName) {
		this.myName = myName;
	}

	public JButton getMyBtnLogin() {
		return myBtnLogin;
	}

	public void setMyBtnLogin(JButton myBtnLogin) {
		this.myBtnLogin = myBtnLogin;
	}

	public JLabel getMyResult() {
		return myResult;
	}

	public void setMyResult(JLabel myResult) {
		this.myResult = myResult;
	}
}
